package com.steven.springbootrabbitmq.controller;

import com.steven.springbootrabbitmq.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author liuxiangyu
 * @date 2021-07-06 22:37
 */
@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {
    @Autowired
    public RabbitTemplate rabbitTemplate;

    //发消息
    @GetMapping("/sendMessage/{message}")
    public void SendMessage(@PathVariable String message) {
        CorrelationData correlationData1 = new CorrelationData("1");
        rabbitTemplate.convertAndSend(
                ConfirmConfig.CONFIRM_EXCHANGE_NAME,
                ConfirmConfig.CONFIRM_ROUTING_KEY_NAME, message, correlationData1);
        log.info("当前时间：{}，队列{}，发送消息：{}", new Date().toString(), ConfirmConfig.CONFIRM_QUEUE_NAME, message);

        CorrelationData correlationData2 = new CorrelationData("2");
        rabbitTemplate.convertAndSend(
                ConfirmConfig.CONFIRM_EXCHANGE_NAME + "123",
                ConfirmConfig.CONFIRM_ROUTING_KEY_NAME, message, correlationData2);
        log.info("当前时间：{}，队列{}，发送消息：{}", new Date().toString(), ConfirmConfig.CONFIRM_QUEUE_NAME, message);

        CorrelationData correlationData3 = new CorrelationData("3");
        rabbitTemplate.convertAndSend(
                ConfirmConfig.CONFIRM_EXCHANGE_NAME,
                ConfirmConfig.CONFIRM_ROUTING_KEY_NAME + "123", message, correlationData3);
        log.info("当前时间：{}，队列{}，发送消息：{}", new Date().toString(), ConfirmConfig.CONFIRM_QUEUE_NAME, message);
        return;
    }
}
